<?php

/**
 * WK  活动专区
 * ============================================================================
 * * 版权所有 2005-2012 QQ 80342014，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2011-01-19 06:29:08Z liubo $
*/

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');

assign_template();


$user = get_user_set($_SESSION['user_id']);

$action  = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'list';

$position = assign_ur_here();
$smarty->assign('page_title',      $position['title']);    // 页面标题
$smarty->assign('ur_here',         $position['ur_here']);  // 当前位置
$smarty->assign('size',     10);
$smarty->assign('user',     $user);
$smarty->assign('user_id',     $_SESSION['user_id']);
$smarty->assign('action',     $action);
if($action == 'list'){

	$smarty->assign('yyqy',        get_activitys_show(2));
	$smarty->assign('wxy',        get_activitys_show(3));
	
	$smarty->assign('acts',        'more');
	$smarty->assign('page_title',        '活动专区');
	$smarty->display('activity_list.dwt');
}
/* 一元购 列表 */
elseif ($action == 'oneyuanbuylist'){
	$smarty->assign('acts',        'more');
	$smarty->assign('page_title',        '活动列表');
	$smarty->display('activity_list.dwt');
	
}
/* 一元购 详情 */
elseif ($action == 'oneyuan_xq'){
	
	$id = isset($_GET['id']) ? intval($_GET['id']) : '';
	if($id <= 0){
		show_message("非法入口，没有有效的ID信息", "返回上一页", 'activitys.php', 'error');
	}
	$sheep = get_sheep_details($id);
	if(empty($sheep) || $sheep['types'] != 2){
		show_message("没有该活动的信息,重新选择", "返回上一页", 'activitys.php', 'error');
	}
	$is_weixin = 0;
	if(strpos( $_SERVER['HTTP_USER_AGENT'],'MicroMessenger') > 0 ){
		$is_weixin = 1;
	}
	$smarty->assign('is_weixin',    $is_weixin);
	// $sheep['jiazhi'] = $sheep['number'] * $sheep['price'];
	
	if($sheep['start_date'] < gmtime() && $sheep['end_date'] > gmtime() ){
		$sheep['start'] = 1;
	} 
 


	
	$smarty->assign('sheep',    $sheep);
	$smarty->assign('acts',        'more');
	 
	/* 模拟中奖 */
	// winning($id);
	
	/* wk 验证当前用户是否有过购买记录 */
	$this_yy = this_yy($_SESSION['user_id'],$id);
	$smarty->assign('this_yy',    $this_yy);
	$smarty->assign('this_count',    count($this_yy));
	
	$zj = get_zj($id);
	$is_winning = 1;/* 未中奖 */
	if($zj){
		if($zj['user_id'] == $_SESSION['user_id'] ){
			$is_winning = 2;
		}
	}
	
 
	$is_kaijiang = 1;
	if($zj || $sheep['end_date'] <= gmtime()){
		$is_kaijiang = 2;
		// if(empty($zj)){
			// $zj['']
		// }
	}
	
	$smarty->assign('is_kaijiang',    $is_kaijiang);
	$smarty->assign('is_winning',    $is_winning);
	$smarty->assign('zj',    $zj);
	
	
	
	
	// echo "<pre>"; print_r($zj);die();
	$smarty->assign('page_title',        '活动详情');
	$smarty->display('activity_list.dwt');
	
}
/* 我想要 列表 */
elseif ($action == 'wxylist'){
	
	/* 验证 当前用户 */
	if($_SESSION['user_id'] <= 0){
		show_message("登录后再来参加活动", "返回上一页", 'user.php', 'error');
	}
	/* 验证是否有 没有成功的活动 */
	$id = $db->getOne("select id from " . $GLOBALS['ecs']->table('sheep_me') ." where user_id = '". $_SESSION['user_id'] ."' and start = 1"); 
	if($id > 0){
		$Loaction = 'activitys.php?act=wantshare_fx&id='.$id; 
		ecs_header("Location: $Loaction\n");
		exit();
	}
 
	$smarty->assign('acts',        'more');
	$smarty->assign('page_title',        '活动列表');
	$smarty->display('activity_list.dwt');
}

/* 我想要 详情 */
elseif ($action == 'wantshare'){
	
	
	
	$id = isset($_GET['id']) ? intval($_GET['id']) : '';
	if($id <= 0){
		show_message("非法入口，没有有效的ID信息", "返回上一页", 'activitys.php', 'error');
	}
	$sheep = get_sheep_details($id);
	if(empty($sheep) || $sheep['types'] != 3){
		show_message("没有该活动的信息,重新选择", "返回上一页", 'activitys.php', 'error');
	}
	$is_weixin = 0;
	if(strpos( $_SERVER['HTTP_USER_AGENT'],'MicroMessenger') > 0 ){
		$is_weixin = 1;
	}
	$smarty->assign('is_weixin',    $is_weixin);
	// $sheep['jiazhi'] = $sheep['number'] * $sheep['price'];
	$smarty->assign('sheep',    $sheep);
	$smarty->assign('acts',        'more');

	$smarty->assign('page_title',        '活动列表');
	$smarty->display('activity_list.dwt');
}

/* 我想要 分享 详情  */
elseif ($action == 'wantshare_fx'){
	
	$id = isset($_GET['id']) ? intval($_GET['id']) : '';
	$lxtype = isset($_GET['lxtype']) ? intval($_GET['lxtype']) : '1';
	$smarty->assign('lxtype',    $lxtype);
	if($id <= 0){
		show_message("这个活动被外星人劫持了", "返回上一页", 'activitys.php', 'error');
	}
	$wantshare = get_wantshare_details($id);
	if(empty($wantshare)){
		show_message("这个活动被外星人劫持了......", "返回上一页", 'activitys.php', 'error');
	}

	$is_weixin = 0;
	if(strpos( $_SERVER['HTTP_USER_AGENT'],'MicroMessenger') > 0 ){
		$is_weixin = 1;
	}
	$smarty->assign('is_weixin',    $is_weixin);

	$count = get_wantshare_count($id);
	$smarty->assign('count',    $count);
 
	$smarty->assign('wantshare',    $wantshare);
	$smarty->assign('acts',        'more');

	$smarty->assign('page_title',        '我想要这只羊,快来帮我砍价吧......');
	$smarty->display('activity_list.dwt');
}



/* 一元购  购买记录表  */
elseif ($action == 'ajax_buy_list'){
	/* 获得当前页码 */
	$page   = !empty($_REQUEST['page'])  && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;	
	/* 获得 羊 */
    $size   =  intval($_GET['size']) ? intval($_GET['size'])  : 4;
    $sheep_id   =  intval($_GET['sheep_id']) ? intval($_GET['sheep_id'])  : 0;
    $count  = get_yy_count($sheep_id);
    $pages  = ($count > 0) ? ceil($count / $size) : 1;
	if ($page > $pages)
    {
        $list = array();
    }else{
		$list = get_yy_count_list( $page, $size ,$sheep_id);
	} 
	$smarty->assign('sheep_id',    $sheep_id);
	$smarty->assign('list',    $list);
	$data = array();
	$data['htmllist'] = $smarty->fetch('library/ajax_buy_list.lbi');
	$data['list'] = $list;
	$data['lengths'] = count($list);
	$data['page'] = $page;
	$data['pages'] = $pages;
	$data['size'] = $size;
	die(json_encode($data));
}

/* 一元购 购买  個人 記錄  */
elseif ($action == 'ajax_buy_xq'){
	$result = array('error' => 0, 'message' => '');
	$sheep_id = isset($_POST['sheep_id']) ? intval($_POST['sheep_id']) : 0;
	$uid = isset($_POST['uid']) ? intval($_POST['uid']) : '';
	if($sheep_id <=0 || $uid <= 0){
		$result['error'] = 1;
		$result['message'] = 'TA是不是没有购买啊？';
		die(json_encode($result));
	}
	$u_user = get_user_set($uid);
	$name = isset($u_user['wx']['nickname']) ? trim($u_user['wx']['nickname']) : $u_user['mobile_phone_mi'] ;
	$this_yy = this_yy($uid,$sheep_id);
	$smarty->assign('name',    $name);
	$smarty->assign('this_yy',    $this_yy);
	$smarty->assign('this_count',    count($this_yy));
	$endtime = $GLOBALS['db']->getOne("select addtime from " .$GLOBALS['ecs']->table('sheep_yy'). " where user_id = '". $uid ."' and sheep_id = '". $sheep_id ."' order by addtime  desc");
	$endtime = local_date($GLOBALS['_CFG']['time_format'], $endtime); 
	$smarty->assign('endtime',    $endtime);
	$data = array();
	$data['htmllist'] = $smarty->fetch('library/ajax_buy_xq.lbi');
	die(json_encode($data));
}

/* 一元购 购买  */
elseif ($action == 'ajax_buy'){
	$result = array('error' => 0, 'message' => '');
	$sheep_id = isset($_POST['sheep_id']) ? intval($_POST['sheep_id']) : 0;
	$pay_type = isset($_POST['pay_type']) ? trim($_POST['pay_type']) : '';
	$pwd = isset($_POST['pwd']) ? trim($_POST['pwd']) : '';
	$pay = array('balance','wxpay');
	if($sheep_id <= 0){
		$result['error'] = 1;
		$result['message'] = '活动是不是进错了. . . . . .';
		die(json_encode($result));
	}
	if(empty($pay_type) || !in_array($pay_type,$pay)){
		$result['error'] = 1;
		$result['message'] = '没有该支付方式';
		die(json_encode($result));
	}
 
	
	$sheep = get_sheep_details($sheep_id);
	/*  */
	$ym = get_activitys_gm_count($sheep_id);
	
	if($ym > $sheep['number']){
		$result['error'] = 1;
		$result['message'] = '就慢了一步，被人抢走了...';
		die(json_encode($result));
	}
	if($pay_type == 'balance'){
		if(empty($pwd) || $pwd != $user['pay_password'] || empty($user['pay_password'])){
			$result['error'] = 1;
			$result['message'] = '您的支付密码错误,若忘记,可进用户中心重置';
			die(json_encode($result));
		}
		
		
		if($user['user_money'] < $sheep['price']){
			$result['error'] = 1;
			$result['message'] = '您的青山羊币不足，充值或者微信支付';
			die(json_encode($result));
		}
		$num = $db->getOne("select count(*) from ". $ecs->table('sheep_yy') ." where user_id = '".  $_SESSION['user_id'] ."' and sheep_id = '". $sheep_id ."'");
		$number = $sheep_id . $_SESSION['user_id']. $num . rand(11111,99999);
		$order = array(
			'sheep_id' => $sheep_id,/* 羊编号 */
			'user_id' => $_SESSION['user_id'] ,/* 用户编号 */
			'addtime' => gmtime(),/* 时间  */
			'number' => $number,/* 随机编号  */
		);
		$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('sheep_yy'), $order, 'INSERT');
		$text = "参加抢购活动：支出 ". $sheep['price'];
		log_account_change($_SESSION['user_id'], -$sheep['price'], 0, 0, 0, $text, ACT_CYHD); // 参与活动支出
		
		/* 处理当前活动的中奖 */
		winning($sheep_id);
		
		$user = get_user_set($_SESSION['user_id']);
		$result['user_money'] = '余额支付(￥'. $user['user_money'] .')';
		$result['error'] = 10;
		$result['message'] = '抢购成功,兑奖码为：'. $number;
		die(json_encode($result));
	}
	
	if($pay_type == 'wxpay'){

		$num = $db->getOne("select count(*) from ". $ecs->table('sheep_yy') ." where user_id = '".  $_SESSION['user_id'] ."' and sheep_id = '". $sheep_id ."'");
		$number = $sheep_id . $_SESSION['user_id']. $num . rand(11111,99999);
		$order = array(
			'sheep_id' => $sheep_id,/* 羊编号 */
			'user_id' => isset($_SESSION['user_id']) ,/* 用户编号 */
			'addtime' => gmtime(),/* 时间  */
			'number' => $number,/* 随机编号  */
		);
		$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('sheep_yy'), $order, 'INSERT');
		/* 处理当前活动的中奖 */
		winning($sheep_id);
		
		$user = get_user_set($_SESSION['user_id']);
		$result['user_money'] = '余额支付';
		$result['error'] = 10;
		$result['message'] = '抢购成功,兑奖码为：'. $number;
		die(json_encode($result));
	}
	
}
/* 发起微信支付  */
elseif ($action == 'ajax_wx_buy'){
	$result = array('error' => 0, 'message' => '');
	$sheep_id = isset($_POST['sheep_id']) ? intval($_POST['sheep_id']) : 0;
	$pay_type = isset($_POST['pay_type']) ? trim($_POST['pay_type']) : '';
	$pay = array('balance','wxpay');
	if($sheep_id <= 0){
		$result['error'] = 1;
		$result['message'] = '活动是不是进错了. . . . . .';
		die(json_encode($result));
	}
	if(empty($pay_type) || !in_array($pay_type,$pay)){
		$result['error'] = 1;
		$result['message'] = '没有该支付方式';
		die(json_encode($result));
	}
	$sheep = get_sheep_details($sheep_id);
	/*  */
	$ym = get_activitys_gm_count($sheep_id);
	
	if($ym > $sheep['number']){
		$result['error'] = 1;
		$result['message'] = '就慢了一步，被人抢走了...';
		die(json_encode($result));
	}
	$pays = $GLOBALS['db']->getRow("select * from ". $GLOBALS['ecs']->table('payment') ." where pay_code = '". $pay_type ."'");
	if(empty($pays)){
		$result['error'] = 1;
		$result['message'] = "没有该支付方式，请联系管理员";
		die(json_encode($result));
	}
	/* wk  如果是微信支付 */
	if($pay_type  == 'wxpay'){
		$return_url = 'http://' . $_SERVER['HTTP_HOST'].'/respond.php&code=wxpay';
		$payment = unserialize($pays['pay_config']);
		define (WXAPPID, $payment['0']['value']); // appid
		define (WXAPPSECRET, $payment['1']['value']); // appSecret
		define (WXMCHID, $payment['2']['value']);
		define (WXKEY, $payment['3']['value']); // 通加密串
		define (NOTIFY_URL, $return_url); // 成功回调url
		include_once("includes/modules/payment/WxPayPubHelper/WxPayPubHelper.php");
		$jsApi = new JsApi_pub();
		$selfUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
		if($_SESSION['user_id']){
			$openid = $GLOBALS['db']->getOne("select wxid from ". $GLOBALS['ecs']->table('users') ." where user_id ='{$_SESSION['user_id']}'");
		}
		$openid = $openid ? $openid : $_SESSION['openids'];
		if(empty($openid)){
			$result['error'] = 1;
			$result['message'] = "当前用户没有绑定微信,请在微信端打开进行支付";
			die(json_encode($result));
		}
		if($_SESSION['user_id'] <= 0){
			$result['error'] = 1;
			$result['message'] = "用户登录超时";
			die(json_encode($result));
		}	
		$unifiedOrder = new UnifiedOrder_pub();
		// 设置统一支付接口参数
		$unifiedOrder->setParameter ( "openid", $openid );
		$unifiedOrder->setParameter ( "body", '司令羊养殖基地' );
		$unifiedOrder->setParameter ( "out_trade_no", $sheep_id.'_'. $_SESSION['user_id'] . gmtime()); // 商户订单号
		$unifiedOrder->setParameter ( "total_fee", $sheep['price'] * 100 ); // 总金额
		// $unifiedOrder->setParameter ( "total_fee", 0.01 * 100 ); // 总金额
		$unifiedOrder->setParameter ( "notify_url", NOTIFY_URL ); // 通知地址
		$unifiedOrder->setParameter ( "trade_type", "JSAPI" ); // 交易类型
	
		$prepay_id = $unifiedOrder->getPrepayId();
		$jsApi->setPrepayId($prepay_id);
		$obj['appId'] = $result['appId2'] = $payment['0']['value'];
		$obj['timeStamp'] = $result['timeStamp'] = time();
		$obj['nonceStr'] = $result['nonceStr2'] = $unifiedOrder->createNoncestr();
		$obj['package'] = $result['packages'] = "prepay_id=".$jsApi->prepay_id;
		$obj['signType'] = $result['signType2'] = 'MD5';
		$result['paySign2'] = $unifiedOrder->getSign($obj);
		$result['message'] = 'success';
		$result['sheep_id'] = $sheep_id;
		$result['error'] = 20;
		die(json_encode($result));
		
	}
	

	
}
/*  ajax 获取 列表 */
elseif ($action == 'ajax_activitys_list'){
	/* 获得当前页码 */
	$page   = !empty($_REQUEST['page'])  && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;	
	/* 获得 羊 */
    $size   =  intval($_GET['size']) ? intval($_GET['size'])  : 4;
    $types   =  intval($_GET['types']) ? intval($_GET['types'])  : 2;
    $count  = get_sheep_count($types);
    $pages  = ($count > 0) ? ceil($count / $size) : 1;
	if ($page > $pages)
    {
        $list = array();
    }else{
		$list = get_sheep_list( $page, $size ,$types);
	} 
	/* 没有数据  返回一个提示信息 */
	$htmllist = '';
	if(!empty($list)){
		foreach($list as &$v){
			$ym = 0;
			$ym = get_activitys_gm_count($v['sheep_id']);
			$v['state'] = '1';
			if($v['start_date'] > gmtime() ){
				$v['text'] = '未开始';
			}elseif($v['start_date'] < gmtime() && $v['end_date'] > gmtime() && $ym < $v['number']){
				$v['text'] = '进行中';
				$v['state'] = '2';
			}else{
				$v['text'] = '已结束';
			}
			$v['number'] -= $ym;
			$v['number'] = $v['number'] > 0 ? $v['number'] : 0;
		}
	} 
	$smarty->assign('types',    $types);
	$smarty->assign('list',    $list);
	$data = array();
	if($types == 2){
		$data['htmllist'] = $smarty->fetch('library/ajax_oneyuanbuylist.lbi');
	}elseif($types == 3){
		$data['htmllist'] = $smarty->fetch('library/ajax_miyao_list.lbi');
	}else{
		$data['htmllist'] = $smarty->fetch('library/ajax_oneyuanbuylist.lbi');
	}
	
	
	$data['list'] = $list;
	$data['lengths'] = count($list);
	$data['page'] = $page;
	$data['pages'] = $pages;
	$data['size'] = $size;
	die(json_encode($data));
}

/*  我想要 购买   */
elseif ($action == 'ajax_me_yao'){
	$result = array('error' => 0, 'message' => '');
	$sheep_id = isset($_POST['sheep_id']) ? intval($_POST['sheep_id']) : 0;
	if($sheep_id <= 0){
		$result['error'] = 1;
		$result['message'] = '活动是不是进错了. . . . . .';
		die(json_encode($result));
	}
	
	if($_SESSION['user_id'] <= 0){
		$result['error'] = 1;
		$result['message'] = '你都不登录,凑什么热闹...';
		die(json_encode($result));
	}
	$sheep = get_sheep_details($sheep_id);
	/*  */
	$ym = get_me_count($sheep_id);
	if($ym > $sheep['number']){
		$result['error'] = 1;
		$result['message'] = '就慢了一步，被人抢走了...';
		die(json_encode($result));
	}
	/*  当前用户是否买过 */
	$u_ym = get_me_count($sheep_id,$_SESSION['user_id']);
	if($u_ym > 0){
		$id = $db->getOne("select id from ". $GLOBALS['ecs']->table('sheep_me')." where sheep_id = '". $sheep_id ."' and user_id = '". $_SESSION['user_id'] ."'");
		$result['id'] = $id;
		$result['error'] = 3;
		$result['message'] = '你都买过了,还来凑什么热闹...';
		die(json_encode($result));
	}
	$order = array(
		'sheep_id' => $sheep_id,/* 羊编号 */
		'user_id' => intval($_SESSION['user_id']) ,/* 用户编号 */
		'addtime' => gmtime(),/* 时间  */
		'money' => $sheep['price'],/* 金额  */
	);
	$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('sheep_me'), $order, 'INSERT');	
	$id = $db->insert_id();
	$result['id'] = $id;
	$result['error'] = 0;
	$result['message'] = '你真幸运,这都被你抢到了,赶快分享给朋友吧';
	die(json_encode($result));
}


/*  我想要  别人帮砍价  */
elseif ($action == 'ajax_kanjia'){
	$result = array('error' => 0, 'message' => '');
	$uid = isset($_POST['user_id']) ? intval($_POST['user_id']) : 0;
	$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
	if($uid <= 0 || $id <= 0){
		$result['error'] = 10;
		$result['message'] = '你是在帮谁砍价呢？';
		die(json_encode($result));
	}
	if($_SESSION['user_id'] <= 0  && empty($_SESSION['openids']) && empty($_SESSION['openid'])){
		$result['error'] = 10;
		$result['message'] = '你都不登录,凑什么热闹...';
		die(json_encode($result));
	}
	/* 验证当前用户 是否 和 该订单的用户的是否一致 */
	$wantshare = get_wantshare_details($id);
	if(empty($wantshare)){
		$result['error'] = 10;
		$result['message'] = '这个活动被外星人劫持了......';
		die(json_encode($result));
	}else{
		$u_user = get_user_set($wantshare['user_id']);
		if($wantshare['user_id'] != $uid){
			$result['error'] = 11;
			$result['message'] = '最讨厌你这样修改参数了......';
			die(json_encode($result));
		}
		
		if($wantshare['user_id'] == $_SESSION['user_id']){
			$result['error'] = 11;
			$result['message'] = '别人来帮你砍价的,你还要砍吗？';
			die(json_encode($result));
		}
		if(!empty($u_user['wxid'])){
			if($u_user['wxid'] == $_SESSION['openids'] || $u_user['wxid'] == $_SESSION['openid']){
				$result['error'] = 11;
				$result['message'] = '别人来帮你砍价的,你还要砍吗？';
				die(json_encode($result));
			}
		}
	}
	/* 验证当前用户是否砍过 */
	if($_SESSION['user_id'] > 0){
		$cut_price = $db->getOne("select money from ". $ecs->table('sheep_me_list') ." where oid = '$id' and user_id = '". $_SESSION['user_id'] ."' and type = 1");
	}else{
		if($_SESSION['openids']){
			$cut_price = $db->getOne("select money from ". $ecs->table('sheep_me_list') ." where oid = '$id' and  openid = '". $_SESSION['openids'] ."' and type = 1");
		}

 
	}
	 
	// $cut_price = $db->getOne("select money from ". $ecs->table('sheep_me_list') ." where oid = '$id' and (user_id = '". $_SESSION['user_id'] ."' or openid = '". $_SESSION['openid'] ."' or openid = '". $_SESSION['openids'] ."') and type = 1");
	
	
	if($cut_price > 0){
		$result['error'] = 11;
		$result['message'] = '你已经砍过了,可以分享出去让别人帮忙砍了';
		die(json_encode($result));
	}
	/* 计算当前可以砍价的 金额 */
	$cut_price  = rand(100,1000)/100;
	if(($wantshare['sy_jine'] - $cut_price) <= 0){
		$cut_price = $cut_price - $wantshare['sy_jine'];
	}
	
	$order = array(
		'oid' => $id,/* 订单 ID  */
		'user_id' => intval($_SESSION['user_id']) ,/* 用户编号 */
		'addtime' => gmtime(),/* 时间  */
		'type' => 1,/* 类型  1 砍价  2 付款   */
		'money' => $cut_price,/* 金额  */
	);
	$dangqian_user = get_user_set($_SESSION['user_id']);
	if($_SESSION['openids']){
		$order['openid'] = $_SESSION['openids'] ;/* 用户 openid  */ 
		$nickname = $_SESSION['wei_info']->nickname;
		$encode = mb_detect_encoding($nickname, array('ASCII','UTF-8','GB2312','GBK','BIG5'));
		$nickname = iconv("$encode",'UTF-8',$nickname);
		$order['name'] = $nickname;
		$order['touxiang'] = $_SESSION['wei_info']->headimgurl;						
	}else{
		if($dangqian_user['wx']['headimgurl']){
			$order['touxiang'] = $dangqian_user['wx']['headimgurl']	;	
		} 
		if($dangqian_user['wx']['nickname']){
			$order['name'] = $dangqian_user['wx']['nickname'];		
		} else{
			$order['name'] = $dangqian_user['mobile_phone_mi'];
		}
	
	}
	
	if(empty($order['user_id']) && empty($order['openid'])){
		$result['error'] = 10;
		$result['message'] = '您未登录也未使用微信访问我们的网站';
		die(json_encode($result));
	}
	$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('sheep_me_list'), $order, 'INSERT');	
	// $id = $db->insert_id();
	$wantshare = get_wantshare_details($id);
	if($wantshare['sy_jine'] <= 0 && $wantshare['start'] == 1){
		/* 订单已经完成 */
		wantshare($id);
	}

	$result['id'] = $id;
	$result['error'] = 0;
	$result['message'] = "成功砍掉". $cut_price ."元";
	die(json_encode($result));

}

/*  砍价列表   */
elseif ($action == 'ajax_kanjia_list'){
	/* 获得当前页码 */
	$page   = !empty($_REQUEST['page'])  && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;	
	/* 获得 羊 */
    $size   =  intval($_GET['size']) ? intval($_GET['size'])  : 4;
    $id   =  intval($_GET['id']) ? intval($_GET['id'])  : 0;
    $lxtype   =  intval($_GET['lxtype']) ? intval($_GET['lxtype'])  : 1;
    $count  = get_wantshare_count($sheep_id);
    $pages  = ($count > 0) ? ceil($count / $size) : 1;
	if ($page > $pages)
    {
        $list = array();
    }else{
		$list = get_wantshare_list( $page, $size ,$id,$lxtype);
	} 
	
	
	$smarty->assign('id',    $id);
	$smarty->assign('list',    $list);
	$data = array();
	$data['htmllist'] = $smarty->fetch('library/ajax_kanjia_list.lbi');
	$data['list'] = $list;
	$data['lengths'] = count($list);
	$data['page'] = $page;
	$data['pages'] = $pages;
	$data['size'] = $size;
	die(json_encode($data));
	echo "<pre>"; print_r($_REQUEST);die();
	
}


/*  微信  支付 砍价   */
elseif ($action == 'ajax_kanjia_wx'){
	$result = array('error' => 0, 'message' => '');
	$uid = isset($_POST['user_id']) ? intval($_POST['user_id']) : 0;
	$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
	$zhanzhu = isset($_POST['zhanzhu']) ? number_format($_POST['zhanzhu']) : 0;
	if($uid <= 0 || $id <= 0){
		$result['error'] = 10;
		$result['message'] = '你是在帮谁支付呢？';
		die(json_encode($result));
	}
	if($_SESSION['user_id'] <= 0  && empty($_SESSION['openids']) && empty($_SESSION['openid'])){
		$result['error'] = 10;
		$result['message'] = '你都不登录,凑什么热闹...';
		die(json_encode($result));
	}
	/* 验证当前用户 是否 和 该订单的用户的是否一致 */
	$wantshare = get_wantshare_details($id);
	if(empty($wantshare)){
		$result['error'] = 10;
		$result['message'] = '这个活动被外星人劫持了......';
		die(json_encode($result));
	}else{
		$u_user = get_user_set($wantshare['user_id']);
		if($wantshare['user_id'] != $uid){
			$result['error'] = 11;
			$result['message'] = '最讨厌你这样修改参数了......';
			die(json_encode($result));
		}
		if(empty($u_user['wxid']) && empty($_SESSION['openids']) && empty($_SESSION['openid'])){
			$result['error'] = 11;
			$result['message'] = '你没有在微信里面,还要继续吗？';
			die(json_encode($result));
		}
	}
	if($zhanzhu <= 0){
		$result['error'] = 11;
		$result['message'] = '这个金额不是合格的数字哦';
		die(json_encode($result));
	}
	if($wantshare['sy_jine'] <= $zhanzhu){
		$zhanzhu = $wantshare['sy_jine'];
	}
	$pays = $GLOBALS['db']->getRow("select * from ". $GLOBALS['ecs']->table('payment') ." where pay_code = 'wxpay'");
	if(empty($pays)){
		$result['error'] = 11;
		$result['message'] = "没有该支付方式，请联系管理员";
		die(json_encode($result));
	}
	
	// include_once('includes/lib_payment.php');
	include_once(ROOT_PATH .'includes/lib_clips.php');
	$log_id = insert_pay_log(gmtime(), $zhanzhu, PAY_HDWXY); /* 羊只付款 */
	
	$return_url = 'http://' . $_SERVER['HTTP_HOST'].'/respond.php&code=wxpay';
	$payment = unserialize($pays['pay_config']);
	define (WXAPPID, $payment['0']['value']); // appid
	define (WXAPPSECRET, $payment['1']['value']); // appSecret
	define (WXMCHID, $payment['2']['value']);
	define (WXKEY, $payment['3']['value']); // 通加密串
	define (NOTIFY_URL, $return_url); // 成功回调url
	include_once("includes/modules/payment/WxPayPubHelper/WxPayPubHelper.php");
	$jsApi = new JsApi_pub();
	$selfUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
	if($_SESSION['user_id']){
		$openid = $GLOBALS['db']->getOne("select wxid from ". $GLOBALS['ecs']->table('users') ." where user_id ='{$_SESSION['user_id']}'");
	}
	$openid = $openid ? $openid : isset($_SESSION['openids']) ? $_SESSION['openids'] : $_SESSION['openid'];
	if(empty($openid)){
		$result['error'] = 1;
		$result['message'] = "当前用户没有绑定微信,请在微信端打开进行支付";
		die(json_encode($result));
	}
	$unifiedOrder = new UnifiedOrder_pub();
	// 设置统一支付接口参数
	$unifiedOrder->setParameter ( "openid", $openid );
	$unifiedOrder->setParameter ( "body", 'GMYZ' );
	$unifiedOrder->setParameter ( "out_trade_no", $log_id.'_'. gmtime()); // 商户订单号
	$unifiedOrder->setParameter ( "total_fee", $zhanzhu * 100 ); // 总金额
	// $unifiedOrder->setParameter ( "total_fee", 0.01 * 100 ); // 总金额
	$unifiedOrder->setParameter ( "notify_url", NOTIFY_URL ); // 通知地址
	$unifiedOrder->setParameter ( "trade_type", "JSAPI" ); // 交易类型

	$prepay_id = $unifiedOrder->getPrepayId();
	$jsApi->setPrepayId($prepay_id);
	$obj['appId'] = $result['appId2'] = $payment['0']['value'];
	$obj['timeStamp'] = $result['timeStamp'] = time();
	$obj['nonceStr'] = $result['nonceStr2'] = $unifiedOrder->createNoncestr();
	$obj['package'] = $result['packages'] = "prepay_id=".$jsApi->prepay_id;
	$obj['signType'] = $result['signType2'] = 'MD5';
	$result['paySign2'] = $unifiedOrder->getSign($obj);
	$result['message'] = 'success';
	$result['log_id'] = $log_id;
	$result['id'] = $id;
	$result['error'] = 20;
	die(json_encode($result));
 
}

/*  微信  支付 砍价 完成   */
elseif ($action == 'ajax_kanjia_wx_done'){
	$result = array('error' => 0, 'message' => '');
	$log_id = isset($_POST['log_id']) ? intval($_POST['log_id']) : 0;
	$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
	$log = $db->getRow("select * from ". $ecs->table('pay_log') ." where log_id = '". $log_id ."' and order_type = '" . PAY_HDWXY  ."' ");
	if(empty($log)){
		$result['error'] = 1;
		$result['message'] = "您应该是没有支付成功的";
		die(json_encode($result));
	}
	
	$cut_price = $log['order_amount'];
	$order = array(
		'oid' => $id,/* 订单 ID  */
		'user_id' => intval($_SESSION['user_id']) ,/* 用户编号 */
		'addtime' => gmtime(),/* 时间  */
		'type' => 2,/* 类型  1 砍价  2 付款   */
		'money' => $cut_price,/* 金额  */
	);
	
	
	$dangqian_user = get_user_set($_SESSION['user_id']);
	if($_SESSION['wei_infos']){
		$order['openid'] = isset($_SESSION['openids']) ? trim($_SESSION['openids']) : isset($_SESSION['openid']) ? trim($_SESSION['openid']) : $dangqian_user['wxid'];/* 用户 openid  */
		
		$nickname = $_SESSION['wei_info']->nickname;
		$encode = mb_detect_encoding($nickname, array('ASCII','UTF-8','GB2312','GBK','BIG5'));
		$nickname = iconv("$encode",'UTF-8',$nickname);
		$order['name'] = $nickname;
		$order['touxiang'] = $_SESSION['wei_info']->headimgurl;						
	}else{
		if($dangqian_user['wx']['headimgurl']){
			$order['touxiang'] = $dangqian_user['wx']['headimgurl']	;	
		} 
		if($dangqian_user['wx']['nickname']){
			$order['name'] = $dangqian_user['wx']['nickname'];		
		} else{
			$order['name'] = $dangqian_user['mobile_phone_mi'];
		}
	
	}

	$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('sheep_me_list'), $order, 'INSERT');	
	// $id = $db->insert_id();
	$wantshare = get_wantshare_details($id);
	if($wantshare['sy_jine'] <= 0 && $wantshare['start'] == 1){
		/* 订单已经完成 */
		wantshare($id);
	}
	$result['id'] = $id;
	$result['error'] = 0;
	$result['message'] = "成功支付". $cut_price ."元";
	die(json_encode($result));

	
}
?>